home *** CD-ROM | disk | FTP | other *** search
- ;
- ; UDP Send code
- ;
- ; This code sends each string to an address (port on a host).
- ;
- ; Written by Anton Reinauer <anton@ww.co.nz.
- ;
- ; Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
- ; for TCP-to-Blitz.
- ;
- ; Turn overflow errors off if running on a 68000
-
-
- WBStartup
- NoCli
- Hostname.s="localhost" ; destination host address
- #PORT=3001 ; destination port to send data to
-
- INCLUDE "TCPFuncs.bb"
- DEFTYPE .w
-
- ;********************************************************
-
- .WriteUDP
- Statement WriteUDP{ad.l,size.w,sendtolen.l}
- SHARED sock.l,host.sockaddrin
-
- ; This routine writes data via UDP
-
- sockwrite.l=0 ;Clear Writemask
- sockwrite.l BitSet sock.l ;set Writemask on our socket
- g=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data
- c=sendto_(sock.l,ad,size,0,host,sendtolen) ;Send data to server
-
- End Statement
-
- .ConnectUDP
- Statement ConnectUDP{host$,port.w}
- SHARED sock.l,host.sockaddrin,hostlen2.l
- ;
- ; Connect to host at specified port
- ; Return true or False if Connection is made
- ;
-
- sock.l=socket_(2,2,0) ; open UDP socket
-
-
- *a.hostent=gethostbyname_(host$) ; set up destination address to send packets to
-
- ;Copy Details to our Sockaddrin structure
-
- bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)
-
- host.sockaddrin\sin_port=port ;Set port number
- host.sockaddrin\sin_family=2 ;Set type to AT_INET
- hostlen2.l=SizeOf.sockaddrin ;Get length of structure sockaddrin
-
- End Function
-
-
- ;****************************************
-
- WbToScreen0
- Window 0,0,20,300,220,$1|$2|$4|$8|$400,"UDP Send",1,2
-
- WindowOutput0
- WindowInput 0
-
- ConnectUDP {Hostname,#PORT} ; open socket and set up destination address
-
- NPrint " Type in data, and hit"
- NPrint " return to send"
-
- ypos=50
- xpos=10
-
- ;****************************************
-
- .Main
-
- Repeat
- Delay_(1)
- b$=Inkey$
- If b$<>""
- If b$=Chr$(13) ;send string on return key
- WLocate 10,30
- Print " "
- xpos=10
-
- t$="SENT: " + send$
- Gosub Print_String
-
- WriteUDP{&send$,Len(send$),hostlen2.l} ; send string
- send$=""
- Else ; build string to send
- send$=send$+b$
- WLocate xpos,30
- xpos+8
- Print b$ ; echo key press
- EndIf
- EndIf
-
- ev.l=Event
-
- Until ev=$200 ; shut down if close gadget hit
-
- CloseTCP{} ; close connection
-
- WLocate 10,30
- Print " "
- WLocate 10,30
- Print"Connection closed"
- Delay_(10)
-
- FreeMem TCPmem,$2000
-
- End
-
- ;****************************************
-
- .Print_String
- ypos+10
- If ypos=200 Then ypos=50
- WLocate 10,ypos
- Print " "
- WLocate 10,ypos
- Print t$ ; print string received
- Return
-
-